﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using DnPakExplorer.Controls;
using DnPakExplorer.Forms;

namespace DnPakExplorer {
	public abstract class AbsItemSlotBox : MyView{

		public PartImage Background { get; set; }

		public PartImage TypeImage { get; set; }

		public PartImage SeletedImage { get; set; }

		public abstract IUserItem Item {
			get;
		}

		public Rectangle ItemDestRect { get; set; }

		public bool Selected { get; set; }

		public override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) {
			if (Item != null && e.X>=0 && e.X<=Size.Width && e.Y>=0 && e.Y <= Size.Height) {
				this.Capture = true;
				InfoOverlayForm.ShowItem(Item);
			} else {
				InfoOverlayForm.HideItem();
				this.Capture = false;
			}
		}

		public override void OnMouseLeave() {
			InfoOverlayForm.HideItem();
			this.Capture = false;
		}

		public override void OnDraw(Graphics g) {
			if (Background != null) {
				Rectangle destRect = Background.DestRect;
				//destRect.X += Bounds.X;
				//destRect.Y += Bounds.Y;
				g.DrawImage(Background.ImageSource, destRect, Background.SourceRect, GraphicsUnit.Pixel);
			}
			if (TypeImage != null) {
				Rectangle destRect = TypeImage.DestRect;
				//destRect.X += Bounds.X;
				//destRect.Y += Bounds.Y;
				g.DrawImage(TypeImage.ImageSource, destRect, TypeImage.SourceRect, GraphicsUnit.Pixel);
			}
			if (Selected && SeletedImage.DestRect != null) {
				Rectangle destRect = SeletedImage.DestRect;
				//destRect.X += Bounds.X;
				//destRect.Y += Bounds.Y;
				g.DrawImage(SeletedImage.ImageSource, destRect, SeletedImage.SourceRect, GraphicsUnit.Pixel);
			}
			if (Item != null) {
				Rectangle destRect = ItemDestRect;
				//destRect.X += Bounds.X;
				//destRect.Y += Bounds.Y;
				Util.DrawItemImage(Item.iconId, destRect, g);
				Util.DrawItemBox(Item.rank, destRect, g);
				Util.DrawItemTags(Item, destRect, g);
			}
		}
	}

	public class ItemSlotBox : AbsItemSlotBox {

		public event OnSlotChangedHandler OnSlotChanged;

		private IItemSlot mSlot;

		public override IUserItem Item {
			get {
				if (TargetSlot != null)
					return TargetSlot.Item;
				return null;
			}
		}

		public int TargetSlotId { get; set; }

		public IItemSlot TargetSlot {
			get {
				return mSlot;
			}
			set {
				IItemSlot old = mSlot;
				mSlot = value;
				if (mSlot != old && OnSlotChanged != null) {
					OnSlotChanged(mSlot, old);
				}
			}
		}

		protected virtual void mSlot_OnItemChanged(IItemSlot slot, IUserItem oldItem) {
		}


		public override void OnDraw(Graphics g) {
			base.OnDraw(g);
			if (Item == null && TargetSlot is MultiplyableSlot) {
				Font font = new Font("微软雅黑", 9.5f, GraphicsUnit.Point);
				StringFormat sf = new StringFormat();
				sf.Alignment = StringAlignment.Center;
				sf.LineAlignment = StringAlignment.Center;
				g.DrawString(((MultiplyableSlot)TargetSlot).Multiply*100+"%", font, Brushes.White, ClientRect, sf);
				//g.DrawString(((MultiplyableSlot)TargetSlot).Multiply*100+"%", font, Brushes.White, Bounds, sf);
			}
			//g.DrawRectangle(Pens.Wheat, ClientRect);
		}

		public delegate void OnSlotChangedHandler(IItemSlot newSlot, IItemSlot oldSlot);
	}

	public class ItemSlotBox1 : ItemSlotBox {
		public int TextOffsetX = 0;
		public int TextOffsetY = 5;

		private List<ItemIcon> mItemIcons = new List<ItemIcon>();

		protected override void mSlot_OnItemChanged(IItemSlot slot, IUserItem oldItem) {
			base.mSlot_OnItemChanged(slot, oldItem);
			mItemIcons.Clear();
			if (Item != null) {
				if (Item.slotsType>0) {
					int posX = ItemDestRect.Right;
					foreach(var djs in Item.AppliedSlotsList){
						ItemIcon icon = new ItemIcon();
						icon.Size = new Size(20, 20);
						icon.Position = new Point(posX, 20);
						posX += 22;
						icon.Item = djs.Item;
						mItemIcons.Add(icon);
					}
				}
			}
		}

		public override void OnDraw(Graphics g) {
			base.OnDraw(g);
			if (Item != null) {
				Font font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
				StringFormat sf = new StringFormat();
				Rectangle rect = new Rectangle(
					Background.DestRect.Width+TextOffsetX,
					TextOffsetY,
					Bounds.Width-Background.DestRect.Width-TextOffsetX,
					Background.DestRect.Height);
				SolidBrush bursh = new SolidBrush(Util.GetRankColor(Item.rank));
				g.DrawString(Item.name, font, bursh, rect, sf);
			}
			foreach (ItemIcon ii in mItemIcons) {
				Point offset = ii.Position;
				g.TranslateTransform(offset.X, offset.Y);
				ii.OnDraw(g);
				g.TranslateTransform(-offset.X, -offset.Y);
			}
		}
	}

	public class ItemSlotBox2 : ItemSlotBox {
		public int TextOffsetX = 2;
		public int TextOffsetY = 0;
		public override void OnDraw(Graphics g) {
			base.OnDraw(g);
			if (Item != null) {
				Font font = new Font("微软雅黑", 7.5f, GraphicsUnit.Point);
				StringFormat sf = new StringFormat();
				Rectangle rect = new Rectangle(
					TextOffsetX,
					Background.DestRect.Height+TextOffsetY,
					Background.DestRect.Width,
					Bounds.Height-Background.DestRect.Height-TextOffsetY);
				SolidBrush bursh = new SolidBrush(Util.GetRankColor(Item.rank));
				g.DrawString(Item.name, font, bursh, rect, sf);
			}
		}
	}

	public class DragonJewelSlotBox : AbsItemSlotBox {

		public override IUserItem Item {
			get {
				if (TargetSlot != null) {
					return TargetSlot.Item;
				} else {
					return null;
				}
			}
		}

		public DragonJewelSlot TargetSlot { get; set; }
	}

	public class ItemIcon {
		public IUserItem Item;
		public Point Position;
		public Size Size;
		public Rectangle Bounds {
			get { return new Rectangle(Position, Size); }
			set {
				Position = value.Location;
				Size = value.Size;
			}
		}

		public Rectangle ClientRect {
			get { return new Rectangle(0, 0, Size.Width, Size.Height); }
		}

		public void OnDraw(Graphics g) {
			if (Item != null) {
				Util.DrawItemBox(Item.rank, ClientRect, g);
				Util.DrawItemImage(Item.iconId, ClientRect, g);
				Util.DrawItemTags(Item, ClientRect, g);
			}
		}
	}
}
